//
//  HWCloudGameController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/14.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import FSPagerView
import JXPageControl
class HWCloudGameController: HWBaseViewController {
    var bgImageView:UIImageView!
    var banner: FSPagerView!
    lazy var codePageControl: JXPageControlJump = {
    let pageControl = JXPageControlJump(frame: CGRect(x: 0, y: 0,width: UIScreen.main.bounds.width,height: 30))
        pageControl.numberOfPages = 0
        pageControl.activeColor = .colorWithHexString(hexString: "#1CD67E")
        pageControl.inactiveColor = .colorWithAlphaHexString(hexString: "#1CD67E", alpha: 0.23)
        pageControl.activeSize = CGSize(width: 14*widthScale, height: 4*widthScale)
        pageControl.inactiveSize = CGSize(width: 14*widthScale, height: 4*widthScale)
        pageControl.columnSpacing = 2*widthScale

        return pageControl
    }()
    var httpManager = HWCloudGameHttpManager()
    var mods:[HWCloudGameModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
    }

}

extension HWCloudGameController {
    private func initUI() -> Void {
        navigationView.titleLabel.text = "云游戏"
        navigationView.titleLabel.textColor = .white
        navigationView.backButton.setImage(UIImage(named: "whiteNavBack"), for: .normal)
        
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "cloudGameBg")
        view.addSubview(bgImageView)
        
        view.bringSubviewToFront(navigationView)
        
        banner = FSPagerView()
        banner.dataSource = self
        banner.delegate = self
        banner.isInfinite = true
        banner.register(HWCloudGamePagerCell.self, forCellWithReuseIdentifier: "HWCloudGamePagerCell")
        banner.automaticSlidingInterval = 0
        banner.interitemSpacing = 9*widthScale
        banner.itemSize = CGSize(width: 322*widthScale, height: 605*widthScale)
        banner.removesInfiniteLoopForSingleItem = true
        view.addSubview(banner)
        
        view.addSubview(codePageControl)
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        banner.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom).offset(29*widthScale)
            make.height.equalTo(605*widthScale)
            make.bottom.equalTo(-85*widthScale)
        }
        
        codePageControl.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(banner.snp.bottom).offset(9*widthScale)
        }
    }
}

extension HWCloudGameController:FSPagerViewDelegate,FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {

        return self.mods.count
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "HWCloudGamePagerCell", at: index) as! HWCloudGamePagerCell
        cell.model = self.mods[index]
        cell.playButtonClick = {
            mod in
            
            self.httpManager.getCloudGameStatus(id: mod.id) { status in
                if status {
                    let webVC = HWWebViewController()
                    webVC.urlString = mod.url
                    HWUniversal.getCurrentVc().navigationController?.pushViewController(webVC, animated: true)
                } else {
                    self.view.showHUD(title: "暂无设备")
                }
            } failure: { errorString in
                self.view.showHUD(title: errorString)
            }

        }
        cell.downButtonClick = {
            mod in
            let detail = HWGameDetailController()
            detail.gameId = mod.gid
            self.navigationController?.pushViewController(detail, animated: true)
        }
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        
        
    }
    
    func pagerViewDidScroll(_ pagerView: FSPagerView) {
        self.codePageControl.progress = pagerView.scrollOffset
    }
    
}


extension HWCloudGameController {
    func loadData() -> Void {
        httpManager.getCloudGame { models in
            self.mods = models
            self.banner.reloadData()
            self.codePageControl.numberOfPages = models.count
        } failure: { error in
            
        }

    }
}
